<template>
  <el-dialog
      :title="type === 'add' ? '添加问题' : '修改问题'"
      v-model="visible"
      width="800px"
      @close="handleClose"
  >
    <el-form :model="ruleForm" :rules="rules" ref="formRef" label-width="100px" class="menu-form">
      <el-form-item label="标题" prop="title">
        <el-input type="text" v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="开始问题" prop="type">
        <el-select
            v-model="ruleForm.topicIds"
            filterable
            remote
            size="large"
            reserve-keyword
            placeholder="请输入题目信息"
            :remote-method="remoteMethod"
            :loading="loading"
        >
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.title"
              :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="description">
        <el-switch v-model="ruleForm.status"
                   active-color="#13ce66"
                   inactive-color="#ff4949"
                   active-value=0
                   inactive-value=1>
        </el-switch>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import {reactive, ref, toRefs} from 'vue'
import {useRoute} from 'vue-router'
import axios from '@/utils/axios'
import {ElMessage, ElSwitch} from 'element-plus'

export default {
  name: 'DialogAddRole',
  components: {
    ElSwitch
  },
  props: {
    type: String,
    reload: Function
  },
  setup(props) {
    const formRef = ref(null)
    const route = useRoute()
    const state = reactive({
      visible: false,
      loading: false,
      ruleForm: {
        title: '',
        states: 0,
        topicIds: []
      },
      options: [],
      rules: {
        title: [
          {required: 'true', message: '标题不能为空', trigger: ['change']}
        ]
      },
      id: ''
    })
    // 获取详情
    const getDetail = (id) => {
      axios.get(`/evaluate/evaluationPlan/${id}`).then(res => {
        state.ruleForm = {...res}
      })
      axios.get("/evaluate/topic", {params: {}}).then(res => {
        state.options = res.map(topic => ({
          id: topic.id,
          title: topic.title
        }))
        state.loading = false
      })
    }
    // 开启弹窗
    const open = (id) => {
      state.visible = true
      if (id) {
        state.id = id
        getDetail(id)
      } else {
        state.ruleForm = {
          title: '',
          description: '',
          type: ""
        }
      }
    }
    // 关闭弹窗
    const close = () => {
      state.visible = false
    }
    const handleClose = () => {
      formRef.value.resetFields()
    }

    const remoteMethod = (query) => {
      if (query) {
        state.loading = true
        axios.get("/evaluate/topic", {params: {query}}).then(res => {
          state.options = res.map(topic => ({
            id: topic.id,
            title: topic.title
          }))
          state.loading = false
        })
      } else {
        state.options = []
      }
    }

    const submitForm = () => {
      formRef.value.validate((valid) => {
        if (valid) {
          if (props.type === 'add') {
            axios.post('/evaluate/evaluationPlan', {...state.ruleForm}).then(() => {
              ElMessage.success('添加成功')
              state.visible = false
              if (props.reload) props.reload()
            })
          } else {
            axios.post('/evaluate/evaluationPlan', {
              id: state.id,
              ...state.ruleForm
            }).then(() => {
              ElMessage.success('修改成功')
              state.visible = false
              if (props.reload) props.reload()
            })
          }
        }
      })
    }
    return {
      ...toRefs(state),
      open,
      close,
      formRef,
      remoteMethod,
      submitForm,
      handleClose
    }
  }
}
</script>